<?php
namespace Organization\Controller;
use Common\Controller\CommonController;

/**
 * 组织模块
 */
class OrganizationController extends CommonController {
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 114;
		$this->_ent = new \Common\Model\EntModel();
		$this->_ent_cust = new \Common\Model\EntCustModel();
		$this->_ent_depart = new \Common\Model\EntDepartmentModel();
	}
	
	
	
	public function allEntStructureAction(){
		$this->_allEntStructure();
	}
	
	
	private function _allEntStructure(){
		$resultData = array();
		$tmp = array();
		$departList = $this->_ent_depart->getAllDepart();
		if(!empty($departList)){
			foreach($departList as $depart){
				$entid = (int)$depart['entid'];
				$pid = (int)$depart['pid'];
				
				$tmp[$entid][$pid][] = array('departid'=>(int)$depart['departid'],'departname'=>$depart['departname']);
			}
		}
		
		$entList = $this->_ent->getAllEnt();
		if(!empty($entList)){
			foreach($entList as &$ent){
				$entid = $ent['entid'];
				$resultData[$entid]['entid'] = (int)$entid;
				$resultData[$entid]['entname'] = $ent['entname'];
				$resultData[$entid]['struct'] = $tmp[$entid];
			}
		}
		
		$this->data = $resultData;
		$this->outPut();
	}
	
	
	
	/**
	 * 全部企业列表
	 */
	public function allEntListAction(){
		$myexist = I('request.myexist');
		$this->_allEntList($myexist);
	}
	
	
	private function _allEntList($myexist){
		if($myexist === "0")
			$resultData = $this->_ent->getNonExistEnt($this->_custInfo['custid']);
		elseif($myexist === "1")
			$resultData = $this->_ent->getEntByCust($this->_custInfo['custid']);
		else
			$resultData = $this->_ent->getAllEnt();
		
		if(empty($resultData)){
			$resultData = array();
		}
		$this->data = $resultData;
		$this->outPut();
	}
	
	
	/**
	 * 我所在和我创建的企业列表
	 */
	public function myEntListAction(){
		$this->_myEntList();
	}
	
	private function _myEntList(){
		$resultData = $this->_ent->getAllEntByCust($this->_custInfo['custid']);
		if(empty($resultData)){
			$resultData = array();
		}else{
			foreach($resultData as &$ent){
				if($ent['custid'] == $this->_custInfo['custid']){
					$ent['_iscreator'] = "1";
				}else{
					$ent['_iscreator'] = "0";
				}
			}
		}
		$this->data = $resultData;
		$this->outPut();
	}
	
	
	/**
	 * 退出企业
	 */
	public function exitEntAction(){
		$entid = (int)I('request.entid');
		$this->_exitEnt($entid);
	}
	
	private function _exitEnt($entid){
		$this->_ent_cust->exitEnt($entid , $this->_custInfo['custid']);
		$this->outPut();
	}
	
	
	/**
	 * 创建企业
	 */
	public function createEntAction(){
		$entname = I('request.entname');
		$mcustid = (int)I('request.mcustid');
		$this->_createEnt($entname , $mcustid);
	}
	
	private function _createEnt($entname , $mcustid){
		if(empty($entname)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$data['entname'] = $entname;
		$data['mcustid'] = $mcustid;
		$data['custid'] = $this->_custInfo['custid'];
		$entid = $this->_ent->addEnt($data);
		if(empty($entid)){
    		$this->status = 3001;
    		$this->msg = L('STATUS_3001');
    		$this->outPut();
		}else{
			$returnData['entid'] = $entid;
			$returnData['entname'] = $entname;
			$this->data = $returnData;
			$this->outPut();
		}
	}
	
	
	/**
	 * 修改企业
	 */
	public function updateEntAction(){
		$entid = (int)I('request.entid');
		$entname = I('request.entname');
		$mcustid = (int)I('request.mcustid');
		$this->_updateEnt($entid , $entname , $mcustid);
	}
	
	private function _updateEnt($entid , $entname , $mcustid){
		if(empty($entid) || empty($entname)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		//检查是否是当前用户创建或管理的企业
		$entInfo = $this->_checkEnt($entid);
		
		$data['entid'] = $entid;
		$data['entname'] = $entname;
		$data['mcustid'] = $mcustid;
		$ret = $this->_ent->updateEnt($data);
		if(empty($ret)){
    		$this->status = 3002;
    		$this->msg = L('STATUS_3002');
    		$this->outPut();
		}
	
		$this->outPut();
	}
	
	
	/**
	 * 获取企业详情
	 */
	public function getEntInfoAction(){
		$entid = (int)I('request.entid');
		$this->_getEntInfo($entid);
	}
	
	private function _getEntInfo($entid){
		if(empty($entid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$entInfo = $this->_ent->getEntById($entid);
		if(empty($entInfo)){
			$this->status = 3011;
			$this->msg = L('STATUS_3011');
			$this->outPut();
		}else{
			$this->data = $entInfo;
		}
	
		$this->outPut();
	}
	
	
	/**
	 * 删除企业
	 */
	public function deleteEntAction(){
		$entid = (int)I('request.entid');
		$this->_deleteEnt($entid);
	}
	
	private function _deleteEnt($entid){
		if(empty($entid)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		//检查是否是当前用户创建的企业
		$entInfo = $this->_ent->getEntById($entid);
		if(empty($entInfo)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		if($entInfo['custid'] != $this->_custInfo['custid']){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$data['entid'] = $entid;
		$ret = $this->_ent->deleteEnt($data);
		if(empty($ret)){
    		$this->status = 3003;
    		$this->msg = L('STATUS_3003');
    		$this->outPut();
		}
	
		$this->outPut();
	}
	
	
	
	/**
	 * 创建部门
	 */
	public function createDepartAction(){
		$departname = I('request.departname');
		$entid = (int)I('request.entid');
		$pid = (int)I('request.pid');
		$mcustid = (int)I('request.mcustid');
		$this->_createDepart($departname , $entid , $pid , $mcustid);
	}
	
	private function _createDepart($departname , $entid , $pid=0 , $mcustid=0){
		if(empty($departname) || empty($entid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		//检查是否是当前用户创建或管理的企业
		$entInfo = $this->_checkEnt($entid);
		
		$data['departname'] = $departname;
		$data['entid'] = $entid;
		$data['pid'] = $pid;
		$data['mcustid'] = $mcustid;
		$departid = $this->_ent_depart->addDepart($data);
		if(empty($departid)){
    		$this->status = 3004;
    		$this->msg = L('STATUS_3004');
    		$this->outPut();
		}else{
			$entChildDepartKey = \cachekey::$key['BASE_KEY_CONF']['ENT_CHILD_DEPART'].$entid.":".$pid;
			$this->_mycache->rm($entChildDepartKey);
			
			$returnData['entid'] = $entid;
			$returnData['departid'] = $departid;
			$returnData['departname'] = $departname;
			$this->data = $returnData;
			$this->outPut();
		}
	
		$this->outPut();
	}
	
	
	/**
	 * 修改部门
	 */
	public function updateDepartAction(){
		$departid = (int)I('request.departid');
		$departname = I('request.departname');
		$entid = (int)I('request.entid');
		$pid = (int)I('request.pid');
		$mcustid = (int)I('request.mcustid');
		$this->_updateDepart($departid , $departname , $entid , $pid , $mcustid);
	}
	
	private function _updateDepart($departid , $departname , $entid , $pid=0 , $mcustid=0){
		if(empty($departid) || empty($departname) || empty($entid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		//检查是否是当前用户创建或管理的部门
		$departInfo = $this->_checkDepart($departid);
		
		$data['departid'] = $departid;
		$data['departname'] = $departname;
		$data['entid'] = $entid;
		$data['pid'] = $pid;
		$data['mcustid'] = $mcustid;
		$ret = $this->_ent_depart->updateDepart($data);
		if(empty($ret)){
    		$this->status = 3005;
    		$this->msg = L('STATUS_3005');
    		$this->outPut();
		}else{
			$entChildDepartKey = \cachekey::$key['BASE_KEY_CONF']['ENT_CHILD_DEPART'].$entid.":".$pid;
			$this->_mycache->rm($entChildDepartKey);
			$this->outPut();
		}
	}
	
	/**
	 * 删除部门
	 */
	public function deleteDepartAction(){
		$departid = (int)I('request.departid');
		$entid = (int)I('request.entid');
		$this->_deleteDepart($departid , $entid);
	}
	
	private function _deleteDepart($departid , $entid){
		if(empty($departid) || empty($entid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		//检查是否是当前用户创建或管理的部门
		$departInfo = $this->_checkDepart($departid);
		
		//检查部门里面是否有子部门或员工,若有则不让删除
		$ret = $this->_ent_depart->getDepartByPid($departid);
		if(!empty($ret)){//还有子部门
			$this->status = 3009;
			$this->msg = L('STATUS_3009');
			$this->outPut();
		}
		
		$ret = $this->_ent_cust->getCustByDepart($departid);
		if(!empty($ret)){//还有员工
			$this->status = 3010;
			$this->msg = L('STATUS_3010');
			$this->outPut();
		}
		
		
		
		$data['departid'] = $departid;
		$data['entid'] = $entid;
		$ret = $this->_ent_depart->deleteDepart($data);
		if(empty($ret)){
    		$this->status = 3006;
    		$this->msg = L('STATUS_3006');
    		$this->outPut();
		}else{
			$entChildDepartKey = \cachekey::$key['BASE_KEY_CONF']['ENT_CHILD_DEPART'].$entid.":".$departInfo['pid'];
			$this->_mycache->rm($entChildDepartKey);
			$this->outPut();
		}
	}
	
	
	/**
	 * 获取子部门
	 */
	public function getDepartByPidAction(){
		$pid = (int)I('request.pid');
		$entid = (int)I('request.entid');
		$this->_getDepartByPid($pid , $entid);
	}
	
	private function _getDepartByPid($pid , $entid){
		if(empty($entid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$entChildDepartKey = \cachekey::$key['BASE_KEY_CONF']['ENT_CHILD_DEPART'].$entid.":".$pid;
		$returnData = null;//$this->_mycache->get($entChildDepartKey);
		
		if(empty($returnData)) $returnData = $this->_ent_depart->getDepartByPid($pid , $entid);
		if(!empty($returnData)){
			foreach($returnData as $depart){
				$departids[] = $depart['departid'];
			}
			
			$childcntArr = array();
			$ret = $this->_ent_depart->getChildCnt($departids);
			if(!empty($ret)){
				foreach($ret as $childcnt){
					$pid = $childcnt['pid'];
					$childcntArr[$pid] = $childcnt['cnt'];
				}
			}
			
			foreach($returnData as &$depart){
				if(!empty($childcntArr[$depart['departid']])){
					$depart['isleaf'] = "0";
				}else{
					$depart['isleaf'] = "1";
				}
			}
			
			$this->_mycache->set($entChildDepartKey , $returnData);
		}
		
		$this->data = $returnData;
		$this->outPut();
	}
	
	
	/**
	 * 将员工加入到某个部门
	 */
	public function addEntCustAction(){
		$departid = (int)I('request.departid');
		$entid = (int)I('request.entid');
		$mobile = I('request.mobile');
		$nickname = I('request.nickname');
		$position = I('request.position');
		$this->_addEntCust($departid , $entid , $mobile , $nickname , $position);
	}
	
	private function _addEntCust($departid , $entid , $mobile , $nickname , $position){
		if(empty($departid) || empty($entid) || empty($mobile)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		//检查是否是当前用户创建或管理的部门
		$departInfo = $this->_checkDepart($departid);
		
		if(empty($this->_cust)) $this->_cust = new \Common\Model\CustModel();
		$result = $this->_cust->getCustInfoByMobile($mobile);
		if(!empty($result)){
			$custid = $result['custid'];
		}else{
			$password = \tools::genRandomNum(6);
	    	$arr = array();
	    	$arr['idenum'] = $this->_getIdeNum();
	    	$arr['mobile'] = $mobile;//手机号注册
	    	$arr['password'] = \tools::encodePwd($password);
	    	$arr['nickname'] = $nickname;
	    	$arr['isonline'] = 1;
	    	$arr['logintimes'] = 0;
	    	$arr['lastloginip'] = \tools::getClientIp();
	    	$arr['lastlogintime'] = date("Y-m-d H:i:s");
	    	$arr['token'] = \tools::getSign($mobile . "_" . time() . "_" . $arr['lastloginip']);
	    	$arr['status'] = C('DEFAULT_CUST_STATUS');
	    	$custid = $this->_cust->addCust($arr);
	    	if(!empty($custid)){
	    		$custExtInfo['custid'] = $custid;
	    		if(empty($this->_cust_ext)) $this->_cust_ext = new \Common\Model\CustExtModel();
	    		$this->_cust_ext->saveCustExt($custExtInfo);
	    		//TODO发送短信告知账号和密码$mobile $password
	    		//\sms::yuntongxun($mobile, array($mobile , $password), 53038);
	    		//\sms::chuanglan($mobile, "您的密码是：{$password}，请登录后进行更改。", true);
	    		//\sms::xunmai($mobile, "您的密码是:{$password}. 请登录后进行更改.如非本人操作,可不用理会! [迅脉优力迪]");
	    	}
		}
		
		$id = 0;
		$data['departid'] = $departid;
		$data['entid'] = $entid;
		$data['custid'] = $custid;
		$data['position'] = $position;
		$id = $this->_ent_cust->addCust($data);
		if(empty($id)){
    		$this->status = 3007;
    		$this->msg = L('STATUS_3007');
    		$this->outPut();
		}else{
			$this->_ent_depart->custNumInr($departid , 1);
			$this->_ent->custNumInr($entid , 1);
			
			//更新redis集合缓存
			$entCustsKey = \cachekey::$key['BASE_KEY_CONF']['ENT_CUSTS'].$entid;
			$this->_mycache->sadd($entCustsKey , $custid);
			
			$departCustsKey = \cachekey::$key['BASE_KEY_CONF']['DEPART_CUSTS'].$departid;
			$this->_mycache->sadd($departCustsKey , $custid);
			
			$entChildDepartKey = \cachekey::$key['BASE_KEY_CONF']['ENT_CHILD_DEPART'].$entid.":".$departid;
			$this->_mycache->rm($entChildDepartKey);
			
		}
	
		$this->outPut();
	}
	
	
	/**
	 * 将员工从某个部门删除
	 */
	public function deleteEntCustAction(){
		$departid = (int)I('request.departid');
		$entid = (int)I('request.entid');
		$custids = I('request.custids');
		$this->_deleteEntCust($departid , $entid , $custids);
	}
	
	private function _deleteEntCust($departid , $entid , $custids){
		if(empty($departid) || empty($entid) || empty($custids)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		//检查是否是当前用户创建或管理的部门
		$departInfo = $this->_checkDepart($departid);
		
		$custids = explode(",", $custids);
		$data['departid'] = $departid;
		$data['entid'] = $entid;
		$data['custids'] = $custids;
		$ret = $this->_ent_cust->deleteCust($data);
		if(empty($ret)){
    		$this->status = 3008;
    		$this->msg = L('STATUS_3008');
    		$this->outPut();
		}else{
			$this->_ent_depart->custNumDecr($departid , (int)$ret);
			$this->_ent->custNumDecr($entid , (int)$ret);
			
			foreach($custids as $custid){
				//更新redis集合缓存
				$entCustsKey = \cachekey::$key['BASE_KEY_CONF']['ENT_CUSTS'].$entid;
				$this->_mycache->srem($entCustsKey , $custid);
					
				$departCustsKey = \cachekey::$key['BASE_KEY_CONF']['DEPART_CUSTS'].$departid;
				$this->_mycache->srem($departCustsKey , $custid);
				
				$entChildDepartKey = \cachekey::$key['BASE_KEY_CONF']['ENT_CHILD_DEPART'].$entid.":".$departid;
				$this->_mycache->rm($entChildDepartKey);
			}
		}
	
		$this->outPut();
	}
	
	/**
	 * 修改员工信息
	 */
	public function updateEntCustAction(){
		$custid = I('request.custid');
		$departid = (int)I('request.departid');
		$entid = (int)I('request.entid');
		$mobile = I('request.mobile');
		$nickname = I('request.nickname');
		$position = I('request.position');
		$this->_updateEntCust($departid , $entid , $custid , $nickname , $position , $mobile);
	}
	
	private function _updateEntCust($departid , $entid , $custid , $nickname , $position , $mobile){
		if(empty($departid) || empty($entid) || empty($custid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		//检查是否是当前用户创建或管理的部门
		$departInfo = $this->_checkDepart($departid);
		
		$data['departid'] = $departid;
		$data['custid'] = $custid;
		$data['nickname'] = $nickname;
		$data['position'] = $position;
		$ret = $this->_ent_cust->updateCust($data);
		if(empty($ret)){
			$this->status = 3008;
			$this->msg = L('STATUS_3008');
			$this->outPut();
		}
	
		$this->outPut();
	}
	
	
	/**
	 * 获取部门下的员工
	 */
	public function getEntCustByDepartAction(){
		$departid = (int)I('request.departid');
		$entid = (int)I('request.entid');
		$this->_getEntCustByDepart($departid , $entid);
	}
	
	private function _getEntCustByDepart($departid , $entid){
		if(empty($departid) || empty($entid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		//检查是否是当前用户所在的企业
		$this->_checkInEnt($entid);
		
		$returnData = $this->_ent_cust->getCustByDepart($departid);
		if(!empty($returnData)){
			foreach($returnData as &$cust){
				$custInfo = $this->_getCustInfo($cust['custid']);
				if(empty($cust['nickname'])){
					$cust['nickname'] = $custInfo['nickname'];
				}
				$cust['mobile'] = $custInfo['mobile'];
			}
		}
	
		$this->data = $returnData;
		$this->outPut();
	}
	
}
